"""
OpenRouter图像输入能力实现
"""
import logging
from typing import Dict, Any, List, Optional, Union

from ...capabilities.image_input import ImageInputCapability

logger = logging.getLogger("connllm.implementations.openrouter.image_input")

class OpenRouterImageInput(ImageInputCapability):
	"""OpenRouter图像输入能力实现"""
	
	def __init__(self, provider):
		"""初始化能力"""
		super().__init__(provider)
	
	def is_supported(self) -> bool:
		"""检查是否支持图像输入能力"""
		# OpenRouter支持图像输入
		return True
	
	def _format_image_message(self, 
							 text: str, 
							 image_data: Dict[str, Any], 
							 role: str, 
							 detail_level: str) -> Dict[str, Any]:
		"""
		格式化OpenRouter图像消息
		
		Args:
			text: 文本内容
			image_data: 图像数据
			role: 消息角色
			detail_level: 图像细节级别
			
		Returns:
			OpenRouter格式的消息字典
		"""
		# 构建多模态消息内容
		content = [
			{
				"type": "text",
				"text": text
			}
		]
		
		# 添加图像内容
		image_url = None
		if image_data["type"] == "url":
			# 直接使用URL
			image_url = image_data["url"]
		else:
			# 将二进制数据转换为base64
			image_url = self._encode_image_base64(
				image_data["data"], 
				image_data["mimetype"]
			)
		
		# 构建图像URL部分
		image_url_obj = {"url": image_url}
		
		# 如果不是auto，添加detail
		if detail_level != "auto":
			image_url_obj["detail"] = detail_level
		
		# 添加图像到内容
		content.append({
			"type": "image_url",
			"image_url": image_url_obj
		})
		
		# 返回完整消息
		return {
			"role": role,
			"content": content
		}
